home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / ANSIshellƒ / os_mac_consoleMW.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-01  |  3.5 KB  |  138 lines  |  [TEXT/MPCC]

  1. /************************************************************************/
  2. /*    Project...:    Standard ANSI-C Library                                    */
  3. /*    Name......:    console.c                                                */
  4. /*    Purpose...:    Stubs for console.c                                        */
  5. /*  Copyright.: ©Copyright 1994 by metrowerks inc. All rights reserved. */
  6. /************************************************************************/
  7.  
  8. #ifndef __CONSOLE__
  9. #include <console.h>
  10. #endif
  11.  
  12. /*
  13.  *    The following four functions provide the UI for the console package.
  14.  *    Users wishing to replace SIOUX with their own console package need
  15.  *    only provide the four functions below in a library.
  16.  */
  17.  
  18. /* I so wish... 04Jan95 e */
  19. #include <stdio.h>
  20. #include <signal.h>
  21. #include <errno.h>
  22. #include <unix.h>
  23. #include "os_mac.h"
  24. extern long gWaitTicksBG;
  25. extern long gWaitTicksFG;
  26.  
  27. Boolean pause_atexit = 1;
  28. unsigned char *console_title = "\peConsole";
  29.  
  30. /*
  31.  *    extern short InstallConsole(short fd);
  32.  *
  33.  *    Installs the Console package, this function will be called right
  34.  *    before any read or write to one of the standard streams.
  35.  *
  36.  *    short fd:        The stream which we are reading/writing to/from.
  37.  *    returns short:    0 no error occurred, anything else error.
  38.  */
  39.  
  40. static WindowPeek eConsole;
  41.  
  42. short InstallConsole(short fd)
  43. {
  44.     if ( eConsole == NULL ) eConsole = os_console_new( console_title );
  45.     _fcreator = CREATOR;
  46.     _ftype = 'BINA'; /* will be set to TEXT for text files */
  47.     return 0;
  48. }
  49.  
  50. /*
  51.  *    extern void RemoveConsole(void);
  52.  *
  53.  *    Removes the console package.  It is called after all other streams
  54.  *    are closed and exit functions (installed by either atexit or _atexit)
  55.  *    have been called.  Since there is no way to recover from an error,
  56.  *    this function doesn't need to return any.
  57.  */
  58.  
  59. extern Boolean gDoQuit;
  60.  
  61. void RemoveConsole(void)
  62. {
  63. #if pauseForQuitP
  64.     /*  pause for user acknowledgement  */
  65.     unsigned char buf[32];
  66.     if ( pause_atexit && !gDoQuit )
  67.     {    SetWTitle( (WindowPtr )eConsole, "\ppress «enter» to exit" );
  68.         while ( os_console_read_nohang( eConsole, buf, 32 ) == 0 ) /* wait */ ;
  69.     }
  70. #endif
  71. }
  72.  
  73. /*
  74.  *    extern long WriteCharsToConsole(char *buffer, long n);
  75.  *
  76.  *    Writes a stream of output to the Console window.  This function is
  77.  *    called by write.
  78.  *
  79.  *    char *buffer:    Pointer to the buffer to be written.
  80.  *    long n:            The length of the buffer to be written.
  81.  *    returns short:    Actual number of characters written to the stream,
  82.  *                    -1 if an error occurred.
  83.  */
  84.  
  85. long WriteCharsToConsole(char *buffer, long n)
  86. {
  87.     return os_console_write( eConsole, (unsigned char *)buffer, n );
  88. }
  89.  
  90. /*
  91.  *    extern long ReadCharsFromConsole(char *buffer, long n);
  92.  *
  93.  *    Reads from the Console into a buffer.  This function is called by
  94.  *    read.
  95.  *
  96.  *    char *buffer:    Pointer to the buffer which will recieve the input.
  97.  *    long n:            The maximum amount of characters to be read (size of
  98.  *                    buffer).
  99.  *    returns short:    Actual number of characters read from the stream,
  100.  *                    -1 if an error occurred.
  101.  */
  102.  
  103. long ReadCharsFromConsole(char *buffer, long n)
  104. { long result = 0;
  105. #if ReadHangsP
  106.   int idle = 0;
  107.   while ((result = os_console_read_nohang( eConsole, (unsigned char *)buffer, n )) == 0)
  108.   {    if (interrupted)
  109.     { interrupted = 0;
  110.       raise( SIGINT );
  111.       errno = EINTR;
  112.       break;
  113.     }
  114.     if ( idle < 8 )
  115.     { idle++;
  116.       gWaitTicksBG <<= 1;
  117.       gWaitTicksFG <<= 1;
  118.     }
  119.   }
  120.   gWaitTicksBG >>= idle;
  121.   gWaitTicksFG >>= idle;
  122. #else
  123.   if ((result = os_console_read_nohang( eConsole, (unsigned char *)buffer, n )) == 0)
  124.   {     result = 0; /* EOF ? */
  125.   }
  126. #endif
  127.   return result;
  128. }
  129.  
  130. /* e */
  131.  
  132. long CharReadyConsoleP(void)
  133. {
  134.   return os_console_chars_ready( eConsole );
  135. }
  136.  
  137. /* end */
  138.